package icegps.com.pingdj.map.lrucache;

import android.graphics.Bitmap;
import android.util.LruCache;

import java.util.Set;
import java.util.TreeSet;

import icegps.com.pingdj.App;
import icegps.com.pingdj.map.utils.TileUtils;

/**
 * Created by 111 on 2018/3/30.
 */

public class BitmapLruCache extends LruCache<String, Bitmap> {

    private long token;
    private Set<String> keys;

    public BitmapLruCache(long token) {
        super(App.getInstance().getMemoryCacheSize());
        keys = new TreeSet<>();
        this.token = token;

    }

    public Set<String> getKeys() {
        return keys;
    }

    @Override
    protected int sizeOf(String key, Bitmap value) {
        return value.getByteCount();
    }

    @Override
    protected void entryRemoved(boolean evicted, String key, Bitmap oldValue, Bitmap newValue) {
        super.entryRemoved(evicted, key, oldValue, newValue);
        if (evicted) {
            TileUtils.saveBitmap(key, oldValue, token);
            keys.remove(key);
        }
    }

    public void add(String key, Bitmap bitmap) {
        put(key, bitmap);
        keys.add(key);
    }


}
